/**
 * Title: AbsSubActivity.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-11-24 上午11:26:46
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.activities;

import android.content.Intent;

/** 
 * ClassName: AbsSubActivity
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2013-11-24 上午11:26:46
 */
public abstract class AbsSubActivity extends BaseActivity {
	private AbsSubActivity requestSubActivity;

	public AbsSubActivity getRequestSubActivity() {
		return requestSubActivity;
	}

	public void setRequestSubActivity(AbsSubActivity requestSubActivity) {
		this.requestSubActivity = requestSubActivity;
	}

	private Class getTargetClass(Intent intent) {
		Class clazz = null;
		try {
			if (intent.getComponent() != null)
				clazz = Class.forName(intent.getComponent().getClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return clazz;
	}

	// 重写了startActivity()方法，
	// 这样调用该方法时会根据目标Activity是否是子Activity而调用不同的方法
	@Override
	public void startActivity(Intent intent) {
		if (getTargetClass(intent) != null
				&& AbsSubActivity.class
						.isAssignableFrom(getTargetClass(intent))) {
			if (this.getParent() instanceof MainActivity) {
				intent.putExtra("fromSubActivity", getClass().getName());
				((MainActivity) this.getParent()).launchNewActivity(intent);
			}
		} else {
			super.startActivity(intent);
		}
	}
	
	/**
	 * 新启动activity
	 * @param intent
	 */
	public void startNewActivity(Intent intent) {
		if (getTargetClass(intent) != null
				&& AbsSubActivity.class
						.isAssignableFrom(getTargetClass(intent))) {
			if (this.getParent() instanceof MainActivity) {
				intent.putExtra("fromSubActivity", getClass().getName());
				((MainActivity) this.getParent()).startActivity(intent);
			}
		} else {
			super.startActivity(intent);
		}
	}

	// 重写了startActivityForResult()方法，
	// 这样调用该方法时会根据目标Activity是否是子Activity而调用不同的方法
	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		intent.putExtra("requestCode", requestCode);
		if (getTargetClass(intent) != null
				&& AbsSubActivity.class
						.isAssignableFrom(getTargetClass(intent))) {
			if (this.getParent() instanceof MainActivity) {
				intent.putExtra("fromSubActivity", getClass().getName());
				((MainActivity) this.getParent())
						.launchNewActivityForResult(this, intent, requestCode);
			}
		} else {
			super.startActivityForResult(intent, requestCode);
		}
	}

	/** 调用此方法来返回上一个界面 */
	public void goback() {
		Class clazz = null;
		try {
			clazz = Class
					.forName(getIntent().getStringExtra("fromSubActivity"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Intent intent = new Intent(this, clazz);
		((MainActivity) this.getParent()).gobackActivity();
	}

	/** 调用此方法来返回上一个界面并返回数据 */
	public void gobackWithResult(int resultCode, Intent data) {
		int requestCode = getIntent().getIntExtra("requestCode", 0);
		Class clazz = null;
		try {
			clazz = Class
					.forName(getIntent().getStringExtra("fromSubActivity"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		data.setClass(this, clazz);
		if (requestSubActivity != null) {
			requestSubActivity.onActivityResult(requestCode, resultCode, data);
		}
		((MainActivity) this.getParent()).gobackActivity();
	}

	public void exitApp()
	{
		((MainActivity) this.getParent()).exitApp();
	}
}
